<template>
  <div class="movie-list-container">
    <div class="header">
      <h1>电影列表</h1>
      <router-link v-if="authStore.isAuthenticated" to="/add-movie" class="add-movie-btn">
        添加电影
      </router-link>
    </div>
    
    <div v-if="loading" class="loading">加载中...</div>
    
    <div v-else class="movies-grid">
      <MovieCard 
        v-for="movie in movies" 
        :key="movie.id" 
        :movie="movie" 
        @click="goToDetail(movie.id)"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import api from '../composables/useApi'
import MovieCard from '../components/MovieCard.vue'

const movies = ref([])
const loading = ref(true)
const router = useRouter()
const authStore = useAuthStore()

onMounted(async () => {
  try {
    const response = await api.getAllMovies()
    movies.value = response.data
  } catch (error) {
    console.error('获取电影列表失败:', error)
  } finally {
    loading.value = false
  }
})

const goToDetail = (id) => {
  router.push({ name: 'movie-detail', params: { id } })
}
</script>

<style scoped>
.movie-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

h1 {
  color: #333;
}

.add-movie-btn {
  padding: 0.5rem 1rem;
  background-color: #42b983;
  color: white;
  border-radius: 4px;
  text-decoration: none;
}

.loading {
  text-align: center;
  padding: 2rem;
  font-size: 1.2rem;
}

.movies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 2rem;
}

@media (max-width: 768px) {
  .movies-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}

@media (max-width: 480px) {
  .movies-grid {
    grid-template-columns: 1fr;
  }
}
</style>